
const fs = require('fs');
const util = require('util');

const path = require('path');

const webpack = require('webpack');
const { RawSource } = webpack.sources

// 将fs.readFile方法变成基于promise风格的异步方法
const readFile = util.promisify(fs.readFile);
class Plugin2 {
  apply(compiler) {
    // 初始化compilation钩子
    compiler.hooks.thisCompilation.tap('Plugin2', (compilation) => {
      // debugger
      // console.log(compilation)

      compilation.hooks.additionalAssets.tapAsync('plugun2', async (cb) => {
        // debugger;
        // console.log(compilation);
        const content = 'test';
        // 往要输出的资源，添加一个a.txt
        compilation.assets['a.txt'] = {
          // 文件大小
          size() {
            return content.length;
          },
          // 文件内容
          source() {
            return content;
          }
        }
        const data = await readFile(path.resolve(path.resolve(__dirname, 'b.txt')));

        // compilation.assets['b.txt'] = new RawSource(data);

        compilation.emitAsset('b.txt', new RawSource(data));
        cb();
      })
    })
  }
}
module.exports = Plugin2;